import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanxiang_app/bean/normal_entity.dart';
import 'package:wanxiang_app/bean/safe_entity.dart';
import 'package:wanxiang_app/utils/constant.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';

class SafeController extends GetxController{

  @override
  void onInit() {
    super.onInit();
    _getSafe();
  }
  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  int resultData = 0;
  String url = '';
  int index = 0;
  List<SafeData> list = [];
  void _getSafe(){

    DioManager.getInstance().post(ServiceUrl.safe, {
    }, (data) async {
      SafeEntity bannerEntity = SafeEntity.fromJson(jsonDecode(data));
      list = bannerEntity.data;
      url = list[0].imgUrl;

      update();
    }, (error) {
      ToastUtil.show(error);
    });
  }
}


class SafePage extends GetView<SafeController>  {
  late SafeController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {

    Get.lazyPut<SafeController>(() => SafeController());
    controller = Get.find();
    context = context1;

    return GetBuilder<SafeController>(
      builder: (_){
        return  Scaffold(
            appBar: MyAppBar(context: context, title: '安全保障',hasLeading: true,),
            backgroundColor: Colors.white,
            body:_content());
      },
    )
    ;
  }
  Widget _content(){
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(top: 10.h,bottom: 20.h),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              margin: EdgeInsets.only(left: 10.w,right: 10.w),
              child:  SingleChildScrollView(
                scrollDirection:Axis.horizontal,
                child: Row(
                  children: _items(),
                ),
              )
            )
           ,
            SizedBox(height: 10.h,),
            controller.url.isNotEmpty?
            Image.network(controller.url,fit:BoxFit.fitWidth):Container()
          ],
        ),
      )
    ) ;
  }

  Widget _tabItem(SafeData item,index){
    return InkWell(
      onTap: (){
        controller.url = item.imgUrl;
        controller.index = index;
        controller.update();
        print(item.imgUrl);
    },
      child: Container(
          margin: EdgeInsets.only(right: 20.w),
          child:  Column(
            children: [
              Text(item.title,style: TextStyle(
              ),),
              SizedBox(height: 5.h,),
              index==controller.index?
              Container(
                height: 2.h,
                width: 60.w,
                color: Color(0xFF007BE5),
              ):Container(height: 2.h,)
            ],
          )
      )
    ) ;
  }
  List<Widget> _items(){
    List<Widget> items = [];
    for(int i=0;i<controller.list.length;i++){
      items.add(_tabItem(controller.list[i],i));
    }

    return items;
  }
}